const fs = require('fs')



/* 读取文件
@param  path :string 文件路径
@return :string 内容
*/
let readFile = async function(path){
  return new Promise(function(resolve, reject){
    fs.readFile(path, 'utf-8', (err, data) => {
      if (err) reject(err)
      resolve(data)
    })
  })
}

/* 创建文件 utf8
@param path :string 文件路径
@param data :string 内容
@return :bool
*/
let writeFile = async function(path, data){
  return new Promise(function(resolve, reject){
    fs.writeFile(path, data, 'utf-8', (err) => {
      if (err) { reject(err) }
      else{
        resolve(true)
      }
    })
  })
}

/* 判断文件是否存在
@param path :string 文件路径
@return :bool
*/
let existsFile = async function(path){
  return new Promise(function(resolve, reject){
    fs.access(path, fs.constants.F_OK, (err) => {
      if (err) { reject(err) }
      else{
        resolve(true)
      }
    })
  })
}





module.exports = {
  readFile: readFile,
  writeFile: writeFile,
  existsFile: existsFile
}